﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;

public partial class TreeViewNoRefreshCsharp : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // This tutorial is provided in part by Server Intellect Web Hosting Solutions http://www.serverintellect.com

        // Visit http://www.AspNetTutorials.com for more ASP.NET Tutorials
       
    }
    protected void Treeview1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        if (e.Node.ChildNodes.Count == 0)
        {
            switch (e.Node.Depth)
            {
                case 0:
                    FillAuthors(e.Node);
                    break;
                case 1:
                    FillTitlesForAuthors(e.Node);
                    break;
            }
        }

    }

    void FillAuthors(TreeNode node)
    {
        string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
        SqlConnection connection = new SqlConnection(connString);
        SqlCommand command = new SqlCommand("Select * From categories where parentid is null", connection);
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataSet authors = new DataSet();
        adapter.Fill(authors);
        if (authors.Tables.Count > 0)
        {
            foreach (DataRow row in authors.Tables[0].Rows)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = row["categoryname"].ToString();
                newNode.Value = (row["categoryid"]).ToString();

               

                newNode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(newNode);
               
                FillTitlesForAuthors(newNode);
            }
        }
    }

    void FillTitlesForAuthors(TreeNode node)
    {
        string authorID = node.Value.ToString();
        string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
        SqlConnection connection = new SqlConnection(connString);
        SqlCommand command = new SqlCommand("Select categoryname, categoryid From categories Where parentid = '" + authorID + "'", connection);
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataSet titlesForAuthors = new DataSet();
        adapter.Fill(titlesForAuthors);
        if (titlesForAuthors.Tables.Count > 0)
        {
            foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = row["categoryname"].ToString();
                newNode.Value = (row["categoryid"]).ToString();
                newNode.PopulateOnDemand = false;
                newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                newNode.SelectAction = TreeNodeSelectAction.None;
                node.ChildNodes.Add(newNode);
                FillTitlesForAuthors(newNode);
            }
        }
    }
}
